import pandas as pd
pd.plotting.register_matplotlib_converters()
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
print("Setup Complete")
Setup Complete
可以使用pd.read_csv
來讀取csv檔案
# Path of the file to read
spotify_filepath = "./spotify.csv"
# Read the file into a variable spotify_data
spotify_data = pd.read_csv(spotify_filepath, index_col="Date", parse_dates=True)
使用head()
可以看到前5筆資料
spotify_data.head()
若要印出最後5筆資料,可以使用tail()
spotify_data.tail()
現在我們已經有資料了,只需要一行就可以將資料轉為圖表
# Line chart showing daily global streams of each song
sns.lineplot(data=spotify_data)
<AxesSubplot:xlabel='Date'>
sns.lineplot
代表說要使用line chart
data=spotify_data
可以選擇要使用的資料
若要設定圖表的寬度及高度,或是圖表的title,可以使用以下的方法
# Set the width and height of the figure
plt.figure(figsize=(14,6))
# Add title
plt.title("Daily Global Streams of Popular Songs in 2017-2018")
# Line chart showing daily golbal streams of each song
sns.lineplot(data=spotify_data)
<AxesSubplot:title={'center':'Daily Global Streams of Popular Songs in 2017-2018'}, xlabel='Date'>
)
我們可以先去找出有那些column可以用
list(spotify_data.columns)
['Shape of You',
'Despacito',
'Something Just Like This',
'HUMBLE.',
'Unforgettable']
選擇Shpae of You
跟Despacito
來繪製line chart
# Set the width and height of the figure
plt.figure(figsize=(14,6))
# Add title
plt.title("Daily Global Streams of Popular Songs in 2017-2018")
# Line chart showing daily global streams of 'Shape of You'
sns.lineplot(data=spotify_data['Shape of You'], label="Shape of You")
# Line chart showing daily global streams of 'Despacito'
sns.lineplot(data=spotify_data['Despacito'], label="Despacito")
# Add label for horizontal axis
plt.xlabel("Date")
Text(0.5, 0, 'Date')